<template>
  <div class="login-container">
    <div class="title-container">
      <p class="title">该项目由北岛项目组开发，欢迎Github联系我们</p>
    </div>
    <el-tabs v-model="activeName">
      <!------登录-------->
      <el-tab-pane label="登录" name="login">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
          <el-form-item prop="username">
            <span class="svg-container">
              <img src="./images/u12.png" alt="">
            </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="账户"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>

          <el-form-item prop="password">
            <span class="svg-container">
              <img src="./images/u16.png" alt="">
            </span>
            <el-input
              ref="password"
              v-model="loginForm.password"
              type="password"
              placeholder="密码"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
          </el-form-item>

          <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>
        </el-form>
      </el-tab-pane>

      <!-------注册------->
      <el-tab-pane label="注册" name="registeref">
        <el-form ref="registerefForm" :model="registerefForm" :rules="registerefRules" class="registeref-form" auto-complete="on" label-position="left">
          <el-form-item prop="username">
            <span class="svg-container">
              <img src="./images/u12.png" alt="">
            </span>
            <el-input
              ref="username"
              v-model="registerefForm.username"
              placeholder="账户"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>

          <el-form-item prop="password">
            <span class="svg-container">
              <img src="./images/u56.png" alt="">
            </span>
            <el-input
              ref="password"
              v-model="registerefForm.password"
              placeholder="密码"
              name="password"
              tabindex="2"
              auto-complete="on"
            />
          </el-form-item>

          <el-form-item prop="email">
            <span class="svg-container">
              <img src="./images/u34.png" alt="">
            </span>
            <el-input
              ref="email"
              v-model="registerefForm.email"
              placeholder="验证邮箱"
              name="email"
              tabindex="2"
              auto-complete="on"
            />
          </el-form-item>

          <el-form-item prop="mobile">
            <span class="svg-container">
              <img src="./images/u34.png" alt="">
            </span>
            <el-input
              ref="mobile"
              v-model="registerefForm.tel"
              placeholder="手机号"
              name="mobile"
              tabindex="2"
              auto-complete="on"
            />
          </el-form-item>

          <el-row>
            <el-col :span="16">
              <el-form-item prop="captcha">
                <el-input
                  ref="password"
                  v-model="registerefForm.yzm"
                  placeholder="验证码"
                  name="captcha"
                  tabindex="2"
                  auto-complete="on"
                />
                <span class="svg-captcha">
                  <img src="./images/u41.png" alt="">
                </span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <!-- <el-button class="getCaptcha">获取验证码</el-button> -->
              <div ref="captcha" class="captcha" style="width:125px;height:40px" />
            </el-col>
          </el-row>

          <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleRegisteref">注册</el-button>
        </el-form>
      </el-tab-pane>
    </el-tabs>

    <div class="foot-container">
      <p style="margin-right:20px;">联系我们</p>
      <p>Gihub</p>
      <p>copyright @ 2020北岛项目组出品</p>
    </div>

  </div>
</template>

<script>
// import { validUsername } from '@/utils/validate'
import { mapActions } from 'vuex'
import { getCaptcha, registeref } from '@/api/user.js'
export default {
  name: 'Login',
  data() {
    return {
      activeName: 'login',
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '请输入用户名' }, { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }, { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }]
      },
      registerefForm: {
        username: '',
        password: '',
        email: '',
        tel: '',
        yzm: ''
      },
      registerefRules: {
        username: [{ required: true, trigger: 'blur', message: '请输入用户名' }, { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }, { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }],
        email: [{ required: false, message: '请输入邮箱地址', trigger: 'blur' }, { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur'] }],
        tel: [{ required: false, trigger: 'blur', message: '手机号不能为空' }, { pattern: '/^1[345789]\d{9}$/', message: '请输入正确的手机号码', trigger: ['blur'] }],
        yzm: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      },
      loading: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
    this.getCaptcha()
  },
  methods: {
    ...mapActions(['user/login']),
    async handleRegisteref() {
      try {
        await registeref(this.registerefForm)
        this.$message.success('注册成功')
        this.registerefForm.username = ''
        this.registerefForm.password = ''
        this.registerefForm.email = ''
        this.registerefForm.tel = ''
        this.registerefForm.yzm = ''
      } catch (error) {
        console.log(error)
      }
    },
    async getCaptcha() {
      const yan = await getCaptcha()
      this.$refs.captcha.innerHTML = yan.data
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  position: relative;
  min-height: 100%;
  width: 100%;
  background: url('./images/u1.jpg') no-repeat;
  background-size: cover;
  overflow: hidden;

.captcha {
  background-color: #fff;
  border-radius: 5px;
  ::v-deep svg {
    width: 125px;
    height: 40px;
  }
}

.el-tabs {
  width: 450px;
  margin: 0 auto;
        ::v-deep.el-tabs__nav {
          width: 100%;
          .el-tabs__item {
            width: 50%;
            padding: 0;
            // color: #fff;
            text-align: center;

          }
        }
  .el-form {
    .el-form-item {
      background-color: #fff;
        color: #D5D5D5;
      .svg-container {
        padding: 10px;
      }
      ::v-deep.el-input {
        height: 40px;
        background-color: #fff;
        .el-input__inner {
          height: 40px;
          padding: 0px 5px;
          background-color: #fff;
          color: #889aa4;
        }
      }
    }
    ::v-deep.el-form-item__content {
      height: 40px;
      line-height: 15px;
    }
  }
}

.svg-captcha {
      position: absolute;
    right: 12px;
    top: 12px;
}
  .getCaptcha {
    margin-left: 20px;
  }
.captcha {
  margin-left: 20px;
}
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 0  ;
    margin: 0 auto;
    overflow: hidden;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 16px;
      color: $light_gray;
      margin: 120px auto 0px auto;
      text-align: center;
      font-weight: normal;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .foot-container {
    color: #fff;
    position: absolute;
    top: 600px;
    left: 50%;
    transform: translate(-50%);
    p {
      display: flex;
    justify-content: center;
    }
  }
}
</style>
